﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>检测是否是闰年</title>
    <script type="text/javascript">
        function checkYear(year){
            //判断是否能被4整除且不能被100整除，还有一个是能被400整除-闰年的标准
            return (((year % 4 === 0) && (year % 100!==0)) || (year % 400 === 0)) ? 1: 0;
        }
        function Judge(form)
        {
            year = form.year.value;
            var Check1 = parseFloat(year);                        //获取由字符串转换成的浮点数
            for (var i = 0; i < year.length; i++) {               //逐字符判断年份的有效性
                var sLetterCheck1 = year.substring(i, i+1);
                if (sLetterCheck1 < "0" || sLetterCheck1 > "9") {
                    alert("请输入一个有效的年份");
                    form.year.focus();
                    form.year.select();
                    return;
                }
            }
            if (year < 1582) {
                form.result.value = "";
                alert("对不起，你输入的年份必须大于1581.");
                form.year.focus();
                form.year.select();
                return;
            }
            checkYear(year);                                      //判断年份是否是闰年
            if (!checkYear(year)) form.result.value = "不是闰年";
            else form.result.value = "是闰年";
        }
    </script>
</head>
<body>
<form>
    检测某一年是否是闰年:<br>
    年份:
    <input type=text name=year size=4>
    <input type=button value="检测是否是闰年" onClick="Judge(this.form);" name="button">
    <input type=text name=result size=15>
</form>
</body>
</html>